export default class {
    constructor({
        dom,
        upCallback
    }) {
        this.dom = dom
        this.unload()
        this.dom.addEventListener('touchstart', this.refreshTouchStart.bind(this));
        this.dom.addEventListener('touchmove', this.refreshTouchMove.bind(this));
        this.dom.addEventListener('touchend', this.refreshTouchEnd.bind(this));
        let divDom = document.createElement('div')
        divDom.className = 'dragContent'
        divDom.innerHTML = '<div class="drag">' + '加载中...' + '</div>'
        this.loadingDom = divDom
        this.upCallback = upCallback
        this.clientHeight = this.dom.clientHeight
    }
    refreshTouchStart(e) {
        let touch = e.changedTouches[0];
        this.startY = touch.clientY; //获得当前按下点的纵坐标
    }
    refreshTouchMove(e) {
        let touch = e.changedTouches[0];
        let _move = this.startY - touch.clientY; //获得滑动的距离
        this.bottomFlag = this.dom.scrollHeight > this.clientHeight && this.dom.scrollHeight - this.clientHeight <= this.dom.scrollTop
        if (this.bottomFlag) {
            this.moveDistance = _move
            if (_move > 0 && _move >= 100) {
                if (!document.querySelector('.dragContent')) {
                    document.querySelector('.course').append(this.loadingDom)
                }
            }
        }
    }
    refreshTouchEnd(e) {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
            if (this.bottomFlag && this.moveDistance >= 100) {
                this.moveDistance = 0
                let dragContent = document.querySelector('.dragContent')
                dragContent && dragContent.remove()
                this.upCallback()
            }
        }, 10);


    }
    unload() {
        this.dom.removeEventListener('touchstart', this.refreshTouchStart.bind(this));
        this.dom.removeEventListener('touchmove', this.refreshTouchMove.bind(this));
        this.dom.removeEventListener('touchend', this.refreshTouchEnd.bind(this));
    }
}